﻿--[[
   @brief    model for wegame mainframe
   @author   hqzheng
   @version  1.0
   @date     2017-04-12
--]]

wegame_mainframe_model = 
{
    nav_tab_type = {
        TAB_HOME = 1,
        TAB_SHOP = 2,
        TAB_VIDEO = 3,
        TAB_NETBAR = 4,
        TAB_TESTZONE = 5,
    };


    last_wnd_size = {
        x = 0,
        y = 0, 
        width = 1280,
        height = 830,
    };

    base_user_type = {
        kUserTypeNormal = 0;
        kUserTypeBigV = 1;
        kUserTypeApp = 2;
        kUserTypeOrg = 3;
        kUserTypeTencent = 4;
        kUserTypeDev = 5;
    };

    dev_user_type = {
        kUserDevNormal = 0;
        kUserDevAgent = 1;
    };

    app_user_type = {
        kUserAppNormal = 0;
        kUserAppAccount = 1;
    };

    game_list_width = 190;
    top_panel_height = 80;

    frame_url = LoadStr("WEGAME_FRAME_INDEX_URL");
    -- frame_url = "https://www.google.com.hk/webhp?hl=zh-CN&gws_rd=ssl";

    is_need_init_frame = true;  -- 是否需要初始化web框架
    web_mainframe_ready = false; -- web框架是否已经准备好
    first_time = 1;
    wait_times = 0;

    current_select_modure_id = GAME_ID.GAME_ID_SHOP;
    start_for_module_id = 0;
    jump_module_web_json = nil; -- web框架初始化完成后跳转到指定的游戏页面

    exit_when_close = 1;
    first_show = true;
    save_size = {};        -- 已保存的大界面的尺寸

    work_area_width = 0;
    work_area_height = 0;

    unvisible_display_change = false;
    login_srv_state = true;     -- 登录tgp服务器是否成功
    default_open_page_setting = nil;
    has_start_for = false;
    is_start_for_login = false;    -- 登录是否带start_for启动
    hase_open_defualt_page = false;
    self_tgp_id = 0;
    self_uin = 0;
    is_self_vip = false;   -- 运营号 
    is_self_big_v = false;
    is_self_game_agent = false;
    is_self_dev = false;
    navigate_tab_menu_setting = {};

    has_get_vip_info = false;
    has_get_vip_game_id = false;
    vip_game_id = 0;
    vip_game_icon = "";
    vip_game_name = "";

    cfg_name = "platform_comm_cfg.js";
    common_cfg = nil;

    wnd_default_witdh = 1280;
    wnd_default_height = 830;

    min_wnd_width = 1150;
    min_wnd_height = 560;

    first_open_web_page = true;

    is_game_list_pack_up = false;
    launcher_start_game = 0;
    cur_pack_up_btn_color_flag = 0; -- 默认是浅色

    is_notify_init_game_list = false;

    preupdate_rule_open = {};

    MANUAL_UPDATE = 0;
    AUTO_UPDATE = 1;
    AUTO_PREUPDATE = 2;

    self_head_url = "";
    is_test_zone_show = 0;  -- 0 不在白名单，1 灰度用户
    has_get_testzone_gray_info = false; 
    has_get_game_authority = false;
    is_blacklist_user = false;
    has_size_record = false;
    page_cache_switch = 1;   -- 页面缓存开关

    screan_width = 0;
    screan_height = 0;

    launch_max_mode_server_key = "lmm";
    launch_max_mode_local_key = "wegame_mainframe.launch_max_mode";

    get_big_v_info = function ()
        wegame_mainframe_model.get_vip_game_info();
    end;

    get_vip_game_info = function ()
        base_utility.log("[wegame_mainframe_model][get_vip_game_info]", true);

        if 0 == wegame_mainframe_model.self_tgp_id then
            return;
        end

        if true == wegame_mainframe_model.has_get_vip_game_id then
            -- 已经取到了则不重复获取
            return;
        end
        local req_data = {};
        local dst_list = {};
        table.insert(dst_list, wegame_mainframe_model.self_tgp_id);
        req_data["uid"] = wegame_mainframe_model.self_tgp_id;
        req_data["dst_list"] = dst_list;

        local url = LoadStr("BT_GET_USER_INFO_URL") .. "?p=" .. 
            base_utility.table_to_json(req_data);
        --url = web_utility.make_login_status_url(url, wegame_mainframe_model.self_uin);
        base_utility.curl_request_async (url,
            "wegame_mainframe_model.on_get_vip_game_info", true, nil, true);
        wegame_mainframe_model.has_get_vip_game_id = true;
    end;

    on_get_vip_game_info = function (json_str)
        base_utility.log("[wegame_mainframe_model][on_get_vip_game_info]json:" .. json_str);
        local result_tb = base_utility.json_to_table(json_str);
        if nil == result_tb then
            return;
        end

        if nil == result_tb.data or 0 ~= result_tb.data.result or nil == result_tb.data.info_list then
           return;
        end

        if #result_tb.data.info_list == 0 then
            return;
        end

        local user_info = result_tb.data.info_list[1];
        if user_info.type == wegame_mainframe_model.base_user_type.kUserTypeBigV then
            wegame_mainframe_model.is_self_big_v = true;
        elseif user_info.type == wegame_mainframe_model.base_user_type.kUserTypeApp then
            wegame_mainframe_model.is_self_vip = true;
            wegame_mainframe_model.vip_game_icon = user_info.poster_url_v;
            wegame_mainframe_model.vip_game_id = user_info.game_id;
            wegame_mainframe_model.vip_game_name = user_info.nick;
            tools_panel.set_self_head(wegame_mainframe_model.vip_game_icon, true);
        elseif user_info.type == wegame_mainframe_model.base_user_type.kUserTypeDev then
            if nil ~= user_info.dev_game_list then
                for index, dev in ipairs(user_info.dev_game_list) do
                    if dev.dev_user_type == wegame_mainframe_model.dev_user_type.kUserDevAgent then
                        wegame_mainframe_model.is_self_game_agent = true;
                        break;
                    end
                end
            end

            if not wegame_mainframe_model.is_self_game_agent then
                wegame_mainframe_model.is_self_dev = true;
            end
        end
    end;

    is_pack_up_game_list = function (game_id)
        local data = {key = "open_game_list"};
        local cfg = comm_center.call_service("i_svr_get_common_cfg", data);
        if nil == cfg then
            return true;
        end
        
        if cfg.value == nil or cfg.value == "" then
            return true;
        end

        local cfg_tb = base_utility.json_to_table(cfg.value);
        local is_pack_up = true;
        for k, v in pairs(cfg_tb) do
            if v.game_id == tostring(game_id) then
                is_pack_up = false;
                break;
            end
        end
        return is_pack_up;
    end;

    get_testzone_gray_info = function ()
        base_utility.log("[wegame_mainframe_model][get_testzone_gray_info]", true);
        if 0 == wegame_mainframe_model.self_tgp_id then
            -- 没取到TGPID则不请求
            return;
        end

        if true == wegame_mainframe_model.has_get_testzone_gray_info then
            -- 已经取到了则不重复获取
            return;
        end

        local url = LoadStr("BT_CHECK_GRAYINFO_URL");
        local req_tb = {};
        local game_list = {};
        table.insert(game_list, GAME_ID.GAME_ID_TGP);
        req_tb["tgpid"] = wegame_mainframe_model.self_tgp_id;
        req_tb["type"] = 1001;
        req_tb["game_list"] = game_list;
        base_utility.curl_request_async(url,
            "wegame_mainframe_model.on_get_testzone_gray_info", false, nil, true, nil, base_utility.table_to_json(req_tb));
        wegame_mainframe_model.has_get_testzone_gray_info = true;
    end;

    on_get_testzone_gray_info = function (json_str)
        base_utility.log("[wegame_mainframe_model][on_get_vip_game_info]json:" .. json_str);
        local result_tb = base_utility.json_to_table(json_str);
        if nil == result_tb then
            return;
        end

        if 0 == result_tb.result and 1001 == result_tb.type
           and wegame_mainframe_model.self_tgp_id == result_tb.tgpid  
           and nil ~= result_tb.game_gray_list then
           for k, v in pairs(result_tb.game_gray_list) do
               if nil ~= v and tonumber(v.gameid) == GAME_ID.GAME_ID_TGP then
                   wegame_mainframe_model.is_test_zone_show = v.is_gray;
                   local gray_info = {is_gray = wegame_mainframe_model.is_test_zone_show};
                   comm_center.send_broadcast("i_broadcast_get_testzone_gray_info", gray_info);
               end
           end
        end
    end;

    get_game_authority = function (game_id)
        base_utility.log("[wegame_mainframe_model][get_is_fn_player]", true);
        local data = {};
        local game_list = {};
        table.insert(game_list, game_id);
        data["game_id_list"] = game_list;

        data["tgpid"] = wegame_mainframe_model.self_tgp_id;

        local url = LoadStr("RAIL_QUERY_USEGAME_BATCH_URL");
        --url = web_utility.make_login_status_url(url, wegame_mainframe_model.self_uin);
        base_utility.curl_request_async (url,
            "wegame_mainframe_model.on_get_game_authority", false, nil, true, nil, base_utility.table_to_json(data));
    end;

    on_get_game_authority = function (json_str)
        local result_tb = base_utility.json_to_table(json_str);
        if nil == result_tb then
            base_utility.inner_log("[on_get_game_authority]result_tb is nil");
            return;
        end
        if 0 == result_tb.result then
            if false == base_utility.table_is_empty(result_tb.user_game_list) then
                -- 有记录则说明有权限，加入到游戏列表
                wegame_mainframe_model.add_fn_to_game_list();
            end
            wegame_mainframe_model.has_get_game_authority = true;
        end
    end;

    is_fn_swtich_open = function ()
        local is_open = false;
        local req_data = {key = "wegame_fn_switch"};
        local data = comm_center.call_service("i_svr_get_common_cfg", req_data);
        if nil ~= data and data.value ~= nil and data.value ~= "" then
           local data_tb = base_utility.json_to_table(data.value);
           if nil ~= data_tb and 1 == data_tb.wegame_fn_switch  then
               is_open = true;
           end
        else
            TGPLog.LogE("is_fn_swtich_open", "query fail.")
        end;
        return is_open;
    end;

    is_in_my_game_list = function (game_id)
        local is_in_list = false;
        local req_data = {};
        req_data["game_id"] = game_id;
        local data = comm_center.call_service("Svr_FindInMyGameList", req_data);
        if nil ~= data and true == data.exist then
            is_in_list = true;
        end
        return is_in_list;
    end;

    is_add_fn_to_game_list = function ()
        local is_add = true; -- 默认添加
        local req_data = {key = "add_fn_to_game_list"};
        local data = comm_center.call_service("i_svr_get_common_cfg", req_data);
        if nil ~= data and data.value ~= nil and data.value ~= "" then
           local data_tb = base_utility.json_to_table(data.value);
           if nil ~= data_tb and 0 == data_tb.add_fn_to_game_list  then
               is_add = false;
           end
        else
            TGPLog.LogE("is_fn_swtich_open", "query fail.")
        end;
        return is_add;
    end;

    add_fn_to_game_list = function ()
        local req_data = {};
        req_data["game_id"] = GAME_ID.GAME_ID_FN;
        req_data["add_front"] = true;
        comm_center.call_service("Svr_AddToGameList", req_data);
    end;

    delete_fn_from_game_list = function ()
        local req_data = {};
        req_data["game_id"] = GAME_ID.GAME_ID_FN;
        req_data["notify"] = true;
        comm_center.call_service("Svr_DelFromGameList", req_data);
    end;

    should_launch_max_mode = function()
        -- 网吧版本默认关闭
        if base_utility.is_netbar_ver() then
            return false;
        end;

        -- oss灰度匹配
        if not wegame_mainframe_model.launch_max_mode_gray_control() then
            return false;
        end;

        -- 不存在记录或已经全屏化过了
        if not wegame_mainframe_model.get_launch_max_mode_local_record() then
            return false;
        end;

        -- 本地记录已进入过全屏模式
        wegame_mainframe_model.set_launch_max_mode_local_record(0);
        -- 后台设置已进入过全屏模式
        wegame_mainframe_model.set_launch_max_mode_cfg(1);
        return true;
    end;

    launch_max_mode_gray_control = function()
        local ret = false;
        local data = {key = "launch_max_mode"};
        local cfg = comm_center.call_service("i_svr_get_common_cfg", data);

        if nil == cfg then
            return ret;
        end
        if cfg.value == nil or cfg.value == "" then
            return ret;
        end
        local cfg_tb = base_utility.json_to_table(cfg.value);
        if cfg_tb == nil then
            return ret;
        end;
        -- 开关关闭的
        if cfg_tb.switch == nil or cfg_tb.switch ~= "1" then
            return ret;
        end;
        
        -- wegame id 尾号匹配
        if cfg_tb.wegame_id_suffix == nil then
            return ret;
        end;

        local match_id = base_utility.get_tgp_id();
        if match_id == nil or match_id == 0 then
            return ret;
        end;
        local str_match_id = tostring(match_id);
        local id_len = string.len(match_id);

        for key, val in pairs(cfg_tb.wegame_id_suffix) do
            if val ~= nil then
                local val_str = tostring(val);
                local val_len = string.len(val_str);

                if id_len >= val_len and val_len > 0 then
                    local tmp = string.sub(str_match_id, -val_len);
                    if tmp == val_str then
                        base_utility.log(
                       "[wegame_mainframe_model][launch_max_mode_gray_control]match gray", true);
                        ret = true;
                        break;
                    end;
                end;
            end;
        end
        
        return ret;
    end;

    get_launch_max_mode_local_record = function()
        local info = {common = 0, key = wegame_mainframe_model.launch_max_mode_local_key};
        local data = comm_center.call_service("Svr_GetConfig", info);
        if data == nil or data.value == nil or data.exits == nil then
            return false;
        end;
        -- 本地没有记录
        if tonumber(data.exits) ~= 1 then
            base_utility.log(
            "[wegame_mainframe_model][get_launch_max_mode_local_record]local not exists", 
            true);
            -- 查询后台配置
            wegame_mainframe_model.get_launch_max_mode_cfg();
            return false;
        end;

        -- 已经进入过启动时全屏模式
        if tonumber(data.value) ~= 1 then
            base_utility.log(
            "[wegame_mainframe_model][get_launch_max_mode_local_record]has local record", 
            true);
            return false;
        end;
        base_utility.log(
        "[wegame_mainframe_model][get_launch_max_mode_local_record]local record launch max mode", 
        true);
        return true;
    end;

    set_launch_max_mode_local_record = function(val)
        base_utility.log(
        "[wegame_mainframe_model][set_launch_max_mode_local_record]val:"..tostring(val), 
        true);
        if val == nil or tostring(val) == nil then
            return;
        end;
        local data = {};
        data["common"] = 0;
        data["key"] = wegame_mainframe_model.launch_max_mode_local_key;
        data["value"] = tostring(val);
        comm_center.call_service("Svr_SetConfig", data);
    end;


    get_launch_max_mode_cfg = function()
        local data = {};
        data["config_list"] = {};
        data["result"] = 0;
        local item = {};
        item.key = wegame_mainframe_model.launch_max_mode_server_key;
        table.insert(data["config_list"], item);
        comm_center.call_service_asyn("i_service_get_cloud_storage_list", data, 
            "wegame_mainframe_model.on_get_launch_max_mode_cfg");
    end;

    on_get_launch_max_mode_cfg = function(data)
        if data == nil or data.result == nil or data.result ~= 0 or data.config_list == nil or
           type(data.config_list) ~= "table" or next(data.config_list) == nil then
           return;
        end;
        base_utility.log(
        "[wegame_mainframe_model][on_get_launch_max_mode_cfg]val:"..base_utility.table_to_json(data), 
        true);
        for index, item in pairs(data.config_list) do
            if item ~= nil and item.key ~= nil and item.value ~= nil and item.exits ~= nil and 
               item.key == wegame_mainframe_model.launch_max_mode_server_key then 
                -- 不存在后台配置,下次登录进入全屏模式
                if item.exits == 0 then
                    wegame_mainframe_model.set_launch_max_mode_local_record(1);
                else
                    wegame_mainframe_model.set_launch_max_mode_local_record(0);
                end
            end;
        end;
    end;

    set_launch_max_mode_cfg = function(val)
        base_utility.log(
            "[wegame_mainframe_model][set_launch_max_mode_cfg]val:"..tostring(val), true);
        local data = {};
        data["config_list"] = {};
        data["result"] = 0;
        local item = {};
        item.key = wegame_mainframe_model.launch_max_mode_server_key;
        item.value = tostring(val);
        table.insert(data["config_list"], item);
        comm_center.call_service_asyn("i_service_set_cloud_storage_list", data, 
            "wegame_mainframe_model.on_set_launch_max_mode_cfg");
    end;

    on_set_launch_max_mode_cfg = function(data)
        base_utility.log(
       "[wegame_mainframe_model][on_set_launch_max_mode_cfg]val:"..base_utility.table_to_json(data),
       true);
    end;

};